﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeChat.Common;
using WeChat.Common2;
using WeChat.HyfPay.Hyflib;
using WeChat.JsSDK.lib;

namespace WeChat.JsSDK.WxPay
{
    public partial class Pay : System.Web.UI.Page
    {
        public static string wxJsApiParam { get; set; } //H5调起JS API参数
        public static string wxJsApiParams { get; set; } //H5调起JS API参数
        public string total_feeShow;   //页面展示商品价格
        public string goodsName;      //页面展示商品名
        public string CompanyName;     //页面展示公司名称
        public static string domainName = Config.domainName;
        protected void Page_Load(object sender, EventArgs e)
        {
            string openid = "";
            if (!IsPostBack)
            {
                if (Request.QueryString["code"] != null || Request.QueryString["code"] != "")
                {
                    string code = Request.QueryString["code"];
                    ApplicationLog.Write("code:" + code);
                    if (Session["openid"] == null || Session["openid"].ToString() == "")
                    {
                        Session["openid"] = OAuthHelper.getOpenid(code);
                    }
                   // openid = "opc0cwB3k-hFwKzwRZ68dp-qIrqs";
                 openid = Session["openid"].ToString();

                    ApplicationLog.Write("获取到的openid:" + openid);
                   
                }
               
                string amount = Request.QueryString["amount"];
                string equipmentID = Request.QueryString["equipmentID"];
                string order_No = Request.QueryString["order_No"];
                string goodsNames = Request.QueryString["goodsName"];
                string Type = Request.QueryString["Type"];
                // ApplicationLog.Write("attach" + attach);
                //检测是否给当前页面传递了相关参数
                //if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee))
                //{
                //    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面传参出错,请返回重试" + "</span>");
                //    div1.Visible = false;
                //    return;
                //}

                total_feeShow = (Double.Parse(amount) / 100).ToString();

                goodsName=goodsNames;
                CompanyName = Config.CompanyName;
             
                //JSAPI支付预处理
                try
                {
#pragma warning disable CS0219 // 变量“attach”已被赋值，但从未使用过它的值
                   string  attach = "addmoney";
#pragma warning restore CS0219 // 变量“attach”已被赋值，但从未使用过它的值
                     wxJsApiParam = HyfPayApi.getResult(Type, order_No, goodsNames, amount, equipmentID, openid);
                  //  wxJsApiParam = WxPayApi.getPayJson(openid, amount, attach, order_No);//获取H5调起JS API参数    

                }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
                catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败，请返回重试" + "</span>");
                    div1.Visible = false;
                }
            }
        }
    }
}